package com.example.cardgame

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class SecondActivity : ComponentActivity() {
    private var imageBitmap: Bitmap? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_second)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val editText = findViewById<EditText>(R.id.editText)
        val editText2 = findViewById<EditText>(R.id.editText2)
        val editText3 = findViewById<EditText>(R.id.editText3)
        val editText4 = findViewById<EditText>(R.id.editText4)
        val button = findViewById<Button>(R.id.button)
        val button2 = findViewById<Button>(R.id.button2)
        val button3 = findViewById<Button>(R.id.button3)
        val checkbox = findViewById<CheckBox>(R.id.checkBox)
        val checkbox2 = findViewById<CheckBox>(R.id.checkBox2)
        val checkbox3 = findViewById<CheckBox>(R.id.checkBox3)
        val checkbox4 = findViewById<CheckBox>(R.id.checkBox4)

        var str = ""
        var str2 = ""
        val radioGroup = findViewById<RadioGroup>(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener { group, checkedId ->
            val radio: RadioButton = findViewById(checkedId)
            str = radio.text.toString()
        }

        button.setOnClickListener {
            val intent = Intent()
            intent.putExtra("name", editText.text.toString())
            intent.putExtra("Email_address", editText2.text.toString())
            intent.putExtra("phone", editText3.text.toString())
            intent.putExtra("Sno", editText4.text.toString())
            intent.putExtra("sex", str)
            if(checkbox.isChecked) {
                str2 += checkbox.text.toString() + " "
            }
            if(checkbox2.isChecked) {
                str2 += checkbox2.text.toString() + " "
            }
            if(checkbox3.isChecked) {
                str2 += checkbox3.text.toString() + " "
            }
            if(checkbox4.isChecked) {
                str2 += checkbox4.text.toString() + " "
            }
            intent.putExtra("hobby", str2)
            imageBitmap?.let { bitmap ->
                intent.putExtra("imageBitmap", bitmap)
            }
            setResult(RESULT_OK, intent)
            finish()
        }
        button2.setOnClickListener {
            finish()
        }
        button3.setOnClickListener {
            dispatchTakePictureIntent()
        }
    }

    private fun dispatchTakePictureIntent() {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if(intent.resolveActivity(packageManager) != null) {
            startActivityForResult(takePictureIntent, 3)
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 3) {
            if (resultCode == RESULT_OK) {
                imageBitmap = data?.extras?.get("data") as Bitmap
                val imageView = findViewById<ImageView>(R.id.imageView2)
                imageView.setImageBitmap(imageBitmap)
            }
        }
    }
}